package com.headline.common;

/**
 * Created by Administrator on 2016/10/20.
 */
public class PagerHelper {
    /**
     * 分页代码
     * 2016/10/20  17:06
     */
    public static String getPager(int objTotalCount, int currentPage, int pageSize, int pageIndexCount, String link, Boolean isShowJump) {
        if (objTotalCount > pageSize) {
            if (currentPage > objTotalCount)
                currentPage = objTotalCount;

            int[] pg = getPagination(objTotalCount, currentPage, pageSize, pageIndexCount);

            StringBuilder s = new StringBuilder();

            s.append("<li class=\"disabled\"><a href=\"#\">" + (currentPage) + "/" + pg[2] + "</a> </li>");
            s.append("<li><a href=\"" + String.format(link, "1") + "\">首页</a></li> ");
            s.append("<li><a href=\"" + String.format(link, (currentPage - 1 > 0 ? (currentPage - 1) : 1)) + "\">上一页</a></li> ");
            for (int i = pg[0]; i <= pg[1]; i++) {
                if (i == currentPage)
                    s.append("<li class=\"active\"><a href=\"" + String.format(link, (i)) + "\">" + (i) + "</a></li> ");
                else
                    s.append("<li><a href=\"" + String.format(link, (i)) + "\">" + (i) + "</a></li> ");
            }
            s.append("<li><a href=\"" + String.format(link, (currentPage + 1 <= pg[2] ? (currentPage + 1) : pg[2])) + "\">下一页</a></li> ");
            s.append("<li><a href=\"" + String.format(link, (pg[2])) + "\">末页</a></li> ");
            if (isShowJump) {
                s.append("<li><input type=\"text\" onkeydown=\"var e = e || window.event;var currKey=e.keyCode||e.which||e.charCode;if(currKey==13 && this.value!='' && this.value!=null && this.value!=undefined){location.href='" + String.format(link, "'+this.value+'") + "';}else if(currKey<48 || currKey>57){return false;}\" onafterpaste=\"this.value=this.value.replace(/[^\\d]/g,'');\" />");
                s.append("<button onclick=\"if(this.previousSibling.value=='' || this.previousSibling.value==null || this.previousSibling.value==undefined){return false;}");
                s.append("else{location.href='" + String.format(link, "'+this.previousSibling.value+'") + "';}\">GO!</button></li>");
            }

            return s.toString();

        } else {
            return "";
        }
    }


    private static int[] getPagination(int objTotalCount, int currentPage, int pageSize, int pageIndexCount) {
        int pageCount = 0;
        int halfPageIndexCount = 0;
        pageCount = (int) Math.ceil((double) objTotalCount / pageSize);
        pageIndexCount = pageIndexCount < 3 ? 3 : pageIndexCount;
        pageIndexCount = (pageIndexCount % 2 == 0) ? pageIndexCount + 1 : pageIndexCount;
        halfPageIndexCount = (pageIndexCount - 1) / 2;

        int min = 0;
        int max = 0;
        if (pageCount <= pageIndexCount) {
            min = 1;
            max = pageIndexCount;
        } else if (currentPage <= halfPageIndexCount) {
            min = 1;
            max = pageIndexCount;
        } else if ((currentPage + halfPageIndexCount) > pageCount) {
            min = pageCount - pageIndexCount + 1;
            max = pageCount;
        } else {
            min = currentPage - halfPageIndexCount;
            max = currentPage + halfPageIndexCount;
        }
        return new int[]{min, max, pageCount};
    }
}
